// strFuncs.h: interface for the strFuncs class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_STRFUNCS_H__E9EF5577_CE05_4409_A2EA_96447DF42F0D__INCLUDED_)
#define AFX_STRFUNCS_H__E9EF5577_CE05_4409_A2EA_96447DF42F0D__INCLUDED_

#include <shlwapi.h>
#pragma comment(lib, "shlwapi")

#include "GlobalConsts.h"

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000


class strFuncs  
{
public:
	
	static BOOL IsGBKFirstByte(const char *szBuf, int x)
	{
		if((BYTE)szBuf[x] > 0x80)
		{
			if(CharPrev(szBuf, szBuf + x + 1) == szBuf + x)
			{
				return TRUE;
			}
		}
		return FALSE;
		
	}
	static BOOL IsGBKSecondByte(const char *szBuf, int x)
	{
		if(x)
		{
			if( ((BYTE)szBuf[x - 1] > 0x80) && (CharPrev(szBuf, szBuf + x) == szBuf + x - 1) )
			{
				return TRUE;
			}
		}
		return FALSE;
	}

	static BOOL isUrlChar(char c)
	{
		return  (c >= '0' && c <='9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || 
				(c == '.') || (c == ':') || (c == '/') || (c == '_') || (c == '-');
	}
	static CString CheckUrl(const char *szBuf, int x, int *px1 = NULL, int *px2 = NULL)
	{
		int x1, x2;
		char c;
		for(x1 = x; x1 >= 0; --x1)
		{
			c = szBuf[x1];
			if(!isUrlChar(c))
			{
				++x1;
				break;
			}
		}
		if(x1 < 0) x1 = 0;

		c = szBuf[x];
		for(x2 = x; ; ++x2)
		{
			c = szBuf[x2];
			if(!isUrlChar(c))
			{
				--x2;
				break;
			}
		}
		if(x2 <= x1)
		{
			return EmptyString;
		}
		if(szBuf[x2] == '.' || szBuf[x1 + 1] == '.')
		{
			return EmptyString;
		}
		for(int n = x1; n <= x2; n++)
		{
			c = szBuf[n];
			if(c == '.')
			{
				break;//if(szBuf[n])
			}
		}
		if(n > x2)
		{
			return EmptyString;
		}

		CString result(szBuf + x1, x2 - x1 + 1);
		if(result.Find("://") == -1)
		{
			result = "http://" + result;
		}
		if(PathIsURL(result))
		{
			if(px1) *px1 = x1;
			if(px2) *px2 = x2;
			return result;
		}
		return EmptyString;
	}

public:
	strFuncs();
	virtual ~strFuncs();

};

#endif // !defined(AFX_STRFUNCS_H__E9EF5577_CE05_4409_A2EA_96447DF42F0D__INCLUDED_)
